import { mgop } from '@aligov/jssdk-mgop'
import { Dialog, Toast } from 'vant'
import store from '@/store'

let mgopRequest = function (config) {
  // do something before request is sent
  let header = { ...config.header }
  header.xtoken = localStorage.getItem('x-token') || ''

  if (store.state.user.userId) {
    header['User-Id'] = store.state.user.userId
  }
  return new Promise((resolve, reject) => {
    mgop({
      ...config,
      header,
      dataType: 'JSON',
      host: 'https://mapi.zjzwfw.gov.cn/',
      appKey: 'qmybmqa8+2001825987+lyrwas', // 必须
      onSuccess: ({ data }) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          Toast({
            message: data.message || 'Error',
            type: 'error',
            duration: 5 * 1000
          })
        }
      },
      onFail: (err) => {
        reject(err)
        console.log(err, 'err')
      }
    })
  })
}

export default mgopRequest
